<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="850px"
    append-to-body
  >
    <div class="form-box">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 类型  编号 手机号码 保管人员 备注 -->
        <!-- <el-form-item label="部门" prop="type">
          <el-select v-model="form.type" placeholder="请选择部门">
            <el-option
              v-for="dict in typeList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictLabel"
            />
          </el-select>
        </el-form-item> -->

        <el-form-item label="归属部门" prop="deptId">
          <treeselectNew
            style="width: 220px"
            v-model="form.deptId"
            :options="deptOptions"
            placeholder="请选择归属部门"
          >
          </treeselectNew>
        </el-form-item>

        <!-- <el-form-item label="游戏类型">
          <el-select v-model="form.gameType" placeholder="请选择游戏类型">
            <el-option
              v-for="item in gameType"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="网易使用状态" v-if="form.gameType == '网易'">
          <el-select v-model="form.wyStatus" placeholder="请选择网易使用状态">
            <el-option
              v-for="dict in gameStatus"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id + ''"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="商品编号"
          prop="custodian"
          v-if="form.gameType == '网易'"
        >
          <el-input v-model="form.wyGoodsId" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item
          label="出售渠道"
          prop="custodian"
          v-if="form.gameType == '网易'"
        >
          <el-input v-model="form.supplyChannel" placeholder="请输入出售渠道" />
        </el-form-item>
        <el-form-item label="下次换绑时间" v-if="form.gameType == '网易'">
          <el-date-picker
            v-model="form.nextTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择下次换绑时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="手机号码" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="运营商">
          <el-select v-model="form.carrier" placeholder="请选择运营商">
            <el-option
              v-for="item in carrierList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="实名归属">
          <el-select v-model="form.custodianType" placeholder="请选择实名归属">
            <el-option
              v-for="item in shimingList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="保管人员" prop="custodian">
          <el-input v-model="form.custodian" placeholder="请输入保管人员" />
        </el-form-item>
        <el-form-item label="卡位分类" prop="category">
          <!-- <el-input v-model="form.category" placeholder="请输入卡位分类" /> -->
          <el-select v-model="form.category" placeholder="请选择卡位类型">
            <el-option
              v-for="(dict, index) in phonecategoryList"
              :key="index"
              :label="dict.remark"
              :value="dict.remark"
            />
          </el-select>
        </el-form-item>

        <!-- <span v-if="billTypeConfig&&billTypeConfig.length">
          <span v-for="custom in billTypeConfig" :key="custom.id">
            <el-form-item :label="custom.name" :prop="custom.key" v-if="custom.isShow">
              <el-input v-model="form[custom.key]" :placeholder="`请填写${custom.name}`" />
            </el-form-item>
          </span>
        </span> -->
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请填写备注"
              :rows="4"
              style="width: 600px"
            />
          </el-form-item>
        </el-row>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button
            type="primary"
            @click="submitForm('form')"
            v-loading="loading"
            >提交</el-button
          >
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import phoneApi from "../../../api/phoneManage/indexn";
import { getDicts } from "@/api/system/dict/data";
import gameListApi from "@/api/gameConfig/gameList";
import { treeselect } from "@/api/system/dept";
import treeselectNew from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "addFina",
  components: { treeselectNew },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value && value.length != 11) {
        callback(new Error("请输入11位手机号"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      loading: false,
      title: "新增手机",
      isEdit: false,
      typeList: [],
      gameType: [],
      gameStatus: [
        { name: "未使用", id: 1 },
        { name: "已空闲", id: 2 },
        { name: "使用中", id: 3 },
        { name: "已售未换绑", id: 4 },
      ],
      billTypeList: [],
      billTypeConfig: null, //自定义字段配置
      uploadImgUrl: "",
      imgShowList: [],
      newImages: [],
      imgFileList: [],

      // 表单参数
      form: {
        deptId: undefined,
        carrier: null,
        custodianType: null,
        category: null,
        num: "",
        tel: "",
        custodian: "",
        remark: "",
        status: 1,
        gameType: "",
        wyStatus: "",
        wyGoodsId: "",
        supplyChannel: "",
        nextTime: "",
      },
      rules: {
        deptId: [{ required: true, message: "请选择部门", trigger: "change" }],
        num: [{ required: true, message: "请输入编号", trigger: "change" }],
        tel: [{ validator: validatePass, trigger: "blur" }],
      },
      deptOptions: [],
      carrierList: [
        { name: "电信", id: 1 },
        { name: "联通", id: 2 },
        { name: "移动", id: 3 },
        { name: "广电", id: 4 },
      ],
      shimingList: [
        { name: "企业卡", id: 1 },
        { name: "公司卡", id: 2 },
        { name: "个人卡", id: 3 },
      ],
      phonecategoryList: [],
    };
  },
  created() {
    // this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
    this.getTypeList();
    this.getGameList();
    this.getTreeselect();
    this.getPhoneCategoryInfo();
  },
  methods: {
    //获取卡位类型
    async getPhoneCategoryInfo() {
      const res = await getDicts("tel_sms_config");
      console.log("res", res);
      this.phonecategoryList = res.data;
    },
    // tenantIdnormalizer(node) {
    //   console.log("node", node);

    //   if (node.children && !node.children.length) {
    //     delete node.children;
    //   }
    //   return {
    //     id: node.id,
    //     label: node.label,
    //     children: node.children,
    //   };
    // },
    /** 查询部门下拉树结构 */
    async getTreeselect() {
      // treeselect().then((response) => {
      //   this.deptOptions = response.data;
      // });
      const res = await treeselect();
      this.deptOptions = res.data;
    },
    // 游戏列表
    getGameList() {
      gameListApi.getGameList().then((res) => {
        this.gameType = res.rows;
      });
    },
    getTypeList() {
      getDicts("tel_department").then((res) => {
        if (res.code == 200) {
          console.log(res);
          this.typeList = res.data;
        }
      });
    },
    // 打开弹窗
    open(flag, data) {
      // this.getTypeList();
      // this.newImages = [];
      // this.imgShowList = [];
      // this.imgFileList = [];
      if (flag == "edit") {
        this.title = "修改手机";
        this.isEdit = true;
        this.handleUpdate(JSON.parse(JSON.stringify(data)));
      } else {
        this.isEdit = false;
      }
      this.dialogVisible = true;
    },

    // 重置
    reset() {
      this.form = {
        deptId: undefined,
        carrier: null,
        custodianType: null,
        num: "",
        tel: "",
        custodian: "",
        category: "",
        remark: "",
        status: 1,
        gameType: "",
        wyStatus: "",
        wyGoodsId: "",
        supplyChannel: "",
        nextTime: "",
      };
      this.$refs.form.resetFields();
    },
    // 获取账单类型列表
    // getTypeList() {
    //   let obj = {
    //     iotype: this.form.iotype
    //   };
    //   financeApi.getBillType(obj).then(res => {
    //     this.billTypeList = res.rows;
    //   });
    // },
    // iotypeChange() {
    //   this.getTypeList();
    // },
    // 账单类型改变
    // billChange(val) {
    //   this.rules = {
    //     billType: [
    //       { required: true, message: "请选择账单类型", trigger: "change" }
    //     ]
    //   }
    //   let ioType = Number(this.form.iotype)
    //   this.$refs.form.resetFields();
    //   this.form.iotype = ioType
    //   this.billTypeList.forEach(item => {
    //     if (item.id == val) {
    //       let arr = JSON.parse(item.filids);
    //       if (arr && arr.length) {
    //         arr.forEach(r => {
    //           if (r.isShow && r.isMust) {
    //             this.rules[r.key] = [
    //               {
    //                 required: true,
    //                 message: `请填写${r.name}`,
    //                 trigger: "blur"
    //               }
    //             ];
    //           }
    //         });
    //         this.$nextTick(() => {
    //           this.billTypeConfig = arr;
    //         });
    //       }
    //     }
    //   });
    // },
    /** 查询详情进行回显 */
    handleUpdate(row) {
      this.form = row;
    },
    submitForm(formName) {
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            phoneApi.editPhone(this.form).then((res) => {
              this.$message.success(res.msg);
              this.reset();
              this.dialogVisible = false;
              this.$parent.getList();
            });
          } else {
            // 新增
            console.log(this.form);
            phoneApi.addPhone(this.form).then((res) => {
              this.$message.success(res.msg);
              this.reset();
              this.dialogVisible = false;
              if (this.$parent.getList) {
                this.$parent.getList();
              } else {
                this.$emit("add");
              }
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    // // 详情图片上传成功
    // handleUploadSuccess(res, file, fileList) {
    //   if (res.code == 200) {
    //     // 取得新上传的文件
    //     this.newImages = fileList.filter(t => {
    //       return t.url.indexOf("blob:") > -1;
    //     });
    //   }
    // },
    // // 详情图片删除
    // handleRemove(response, fileList, file) {
    //   const that = this;
    //   // 1.获取将要删除图片的临时路径
    //   const filePath = response.url;
    //   // 2.找到图片对应的索引值
    //   this.imgFileList.forEach(function(element, key) {
    //     if (element.url === filePath) {
    //       that.imgFileList.splice(key, 1); //回显的图片
    //     }
    //   });
    //   this.newImages.forEach(function(element, key) {
    //     if (element.url === filePath) {
    //       that.newImages.splice(key, 1); //新上传的图片
    //     }
    //   });
    // },
    // // 图片预览
    // handlePictureCardPreview(file) {
    //   this.$viewerApi({
    //     images: [file.url]
    //   });
    // },
    // uploadNumber(file, fileList) {
    //   //限制图片数量
    //   this.$modal.msgError("最多只能上传50张图片");
    // },
    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.form-box {
  max-height: calc(100vh - 20vh - 100px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-top: 20px;
}
.form-box >>> .el-input {
  width: 250px;
}

.form-box >>> img {
  width: 100%;
}
.form-box >>> .el-upload-dragger {
  width: 148px;
  height: 148px;
}
>>> .el-dialog__body {
  padding: 10px;
}
.form-box >>> .el-form-item__content {
  max-width: 655px;
}
</style>
